﻿Option Explicit On
Option Strict On
Imports System.Runtime.InteropServices.Marshal
Public Class ScrollBarRestore
    '===================================================================
    ' API Function: GetScrollPos
    '===================================================================
    Private Declare Function GetScrollPos Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nBar As Integer) As Integer
    '===================================================================
    ' API Function: PostMessageA
    '===================================================================
    Private Declare Function PostMessageA Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Boolean

    '===================================================================
    ' for NativeWindow and PostMessageA
    '===================================================================
    Private Const WM_HSCROLL As Integer = &H114
    Private Const WM_VSCROLL As Integer = &H115
    Private Const WM_MOUSEWHEEL As Integer = &H20A
    Private Const WM_COMMAND As Integer = &H111
    Private Const WM_USER As Integer = &H400
    '===================================================================
    ' for GetScroll and PostMessageA
    '===================================================================
    Private Const SBS_HORZ As Integer = 0
    Private Const SBS_VERT As Integer = 1
    Private Const SB_THUMBPOSITION As Integer = 4
    Private RTB1Position As Integer
    Public Sub GetScroll(ByVal Handle As IntPtr)
        Me.RTB1Position = GetScrollPos(Handle, SBS_VERT)
    End Sub
    Public Sub SetScroll(ByVal Handle As IntPtr)
        PostMessageA(Handle, WM_VSCROLL, SB_THUMBPOSITION + &H10000 * Me.RTB1Position, 0)
    End Sub
End Class
